package com.itany.shop.controller;

import com.itany.shop.entity.User;
import com.itany.shop.exception.CodeErrorException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:石小俊
 * @Date:2022/12/15 11:14
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/register.user".equals(path)){
            register(request,response);
        }
        if("/login.user".equals(path)){
            login(request,response);
        }
        if("/logout.user".equals(path)){
            logout(request,response);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 当用户注销时,实现两个功能
        // 1.注销用户
        // 2.注销购物车
        // 当前session有且仅有这两个
        // 因此,可以直接清空session
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath()+"/findAll.product");
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        // 获取表单的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        // 获取图片中的验证码,保存在session中
        String image = (String) request.getSession().getAttribute("code");

        try {
            User user = userService.login(username,password,code,image);
            // 用于登录的用户信息在系统中是经常需要使用的
            // 因此,将登录的用户信息保存到session中,便于使用
            request.getSession().setAttribute("user",user);

            // 当用户登录成功之后,会为每一个登录成功的用户分配一个购物车,便于购物
            // 购物车在系统也是经常使用,因此,同样保存到session中
            request.getSession().setAttribute("cart",new Cart());

            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (CodeErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (UserNotExistException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        // 获取表单提交的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        try {
            // 调用service
            userService.register(username,password,phone,address);
            response.sendRedirect(request.getContextPath()+"/jsp/login.jsp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (UserExistException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("registerMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
        }
    }


}
